<?php

namespace app\controllers;
use app\models\Country;
use app\models\News;
use app\models\NewsTbl;
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use \yii\db\Query;

class NewsController extends Controller
{
//    public $enableCsrfValidation = false;
    public function actionIndex()
    {
        $model = new NewsTbl();

        //$subQuery = (new Query())->select('COUNT(*)')->from('news_tbl')->one();
        //print_r($subQuery);exit;


//        $rows = (new \yii\db\Query())
////            ->select(['news_id', 'news_title', 'news_company'])
//            ->from('news_tbl')
////            ->where(['cateId' => '2'])
//
////            ->limit(10)
//            ->orderBy('news_id asc')
//            ->all();
//
//        $rows = NewsTbl::findAll([]);
        $rows = (new \yii\db\Query())
            ->select(['news_id', 'news_title', 'news_content'])
            ->from('news_tbl')
            ->where(['cateId' => '2'])
            ->limit(10)
            ->all();


        return $this->render('index', [
            'newsList' => $rows
        ]);
    }

    public function actionAdd()
    {

        if ($this->request->isPost) {
            $data=Yii::$app->request->post('NewsTbl');
            $m = new NewsTbl();
            $m->cateId = $data['cateId'];
            $m->news_title = $data['news_title'];
            $m->newImg = $data['newImg'];
            $m->news_company=$data['news_company'];;
            $m->newsTime = $data['newsTime'];;
            $m->news_content = $data['news_content'];;
            $m->Report_Mailbox = $data['Report_Mailbox'];;
            $m->like_count = $data['like_count'];;
            $m->comment_count = $data['comment_count'];;
            $m->collect_count = $data['collect_count'];;
            $m->forward_count = $data['forward_count'];;
            $m->createTime = time();
            $m->updateTime = time();

//            $data = [
//                'cateId' => $cateId,
//                'news_title' => $news_title,
//                'newImg' => $newImg,
//                'news_company'=>$news_company,
//                'newsTime' => $newsTime,
//                'news_content' => $news_content,
//                'Report_Mailbox' => $Report_Mailbox,
//                'like_count' => $like_count,
//                'comment_count' => $comment_count,
//                'collect_count' => $collect_count,
//                'forward_count' => $forward_count,
//            ];
//            $m->setAttributes($data);

            $m->save();
            var_dump($m->getErrors());exit;
        }

        return $this->render('_form',
            [
                'model' => new NewsTbl(),
            ]);
    }

    public function actionView($news_id)
    {
        return $this->render('view', [
            $this->module->id => $news_id,
        ]);
    }

    public function actionCreate()
    {

               $model = new News();
                if (Yii::$app->request->isPost) {
                    if ($model->load(Yii::$app->request->post()) && $model->validate()) {

                    }
                }
    }

    public function actionUpdate()
    {
        $id = Yii::$app->request->get('id');
        $result = NewsTbl::find()->where(['news_id' => $id])->asArray()->one();

        if ($this->request->isPost) {
            $title = Yii::$app->request->post('news_title');
            $content = Yii::$app->request->post('news_content');

            NewsTbl::updateAll(['news_title' => $title, 'news_content' => $content], ['news_id' => $id]);

            $this->redirect(['news/index']);
        }

        //$result = NewsTbl::findOne($id);

        return $this->render('update', [
            'model' => $result,
        ]);
    }

    public function actionDel()
    {
        $id = Yii::$app->request->get('id');
        NewsTbl::deleteAll(['news_id' => $id]);
        $this->redirect(['news/index']);
    }
}